import { da } from 'element-plus/es/locales.mjs'
import request from '../../../utils/request'
import {
  UserResponseData,
  User,
  AllRoleResponseData,
  SetRoleData,
} from './type'
enum API {
  // 获取所有已有管理用户
  ALLUSER_URL = '/admin/acl/user/',
  // 添加管理用户
  ADDUSER_URL = '/admin/acl/user/save',
  // 修改管理用户
  UPDATEUSER_URL = '/admin/acl/user/update',
  // 根据用户获取角色数据
  ALLROLE_URL = '/admin/acl/user/toAssign/',
  // 根据用户分配角色
  DOASSIGNROLE_URL = '/admin/acl/user/doAssignRole',
  // 删除管理用户_批量删除
  USERBATCHDELETE_URL = '/admin/acl/user/batchRemove',
  // 删除管理用户_删除用户
  USERDELETE_URL = '/admin/acl/user/remove/',
}
/* 获取已有用户信息 */
export const reqUserInfo = (page: number, limit: number, username: string) =>
  request.get<any, UserResponseData>(
    API.ALLUSER_URL + `${page}/${limit}/?username=${username}`,
  )

/* 添加或者修改一个用户 */
export const reqAddOrUpdateUser = (user: User) => {
  if (user.id) {
    return request.put<any, any>(API.UPDATEUSER_URL, user) //入参有id修改用户
  } else {
    return request.post<any, any>(API.ADDUSER_URL, user) //入参没有id就添加用户
  }
}

/* 根据用户获取角色数据 */
export const reqAllRole = (id: number) =>
  request.get<any, AllRoleResponseData>(API.ALLROLE_URL + id)

/*  分配角色 */
export const reqAssignRole = (data: SetRoleData) =>
  request.post<any, any>(API.DOASSIGNROLE_URL, data)

/* 删除管理用户_根据id删除 */
export const reqDeleteUsers = (data) =>
  request.delete<any, any>(API.USERDELETE_URL + data)

/* 删除管理用户__批量删除 */
export const reqDeleteSelectedUser = (idList: number[]) =>
  request.delete(API.USERBATCHDELETE_URL, { data: idList }) // { data: idList }为什么不能写成idList
